/*
 * @Author: wangkg wangkegui@longfor.com
 * @Date: 2024-04-09 09:41:06
 * @Description:
 */
import { ElMessage as message } from 'element-plus';

import { useUserStoreWithOut } from '@/store/modules/user';

const error = (config: any) => {
  return message({
    type: 'error',
    ...config
  });
};

export function checkStatus(status: number, msg: string): void {
  const userStore = useUserStoreWithOut();
  let errMessage = '';

  switch (status) {
    case 400:
      errMessage = `${msg}`;
      break;
    // 401: Not logged in
    // Jump to the login page if not logged in, and carry the path of the current page
    // Return to the current page after successful login. This step needs to be operated on the login page.
    case 401:
      userStore.setToken(undefined);
      errMessage = msg || '用户没有权限（令牌、用户名、密码错误）!';
      break;
    case 403:
      errMessage = '用户得到授权，但是访问是被禁止的。!';
      break;
    // 404请求不存在
    case 404:
      errMessage = '网络请求错误,未找到该资源!';
      break;
    case 405:
      errMessage = '网络请求错误,请求方法未允许!';
      break;
    case 408:
      errMessage = '网络请求超时!';
      break;
    case 500:
      errMessage = '服务器错误,请联系管理员!';
      break;
    case 501:
      errMessage = '网络未实现!';
      break;
    default:
  }

  if (errMessage) {
    error({ message: errMessage, key: `global_error_message_status_${status}` });
  }
}
